package com.sugon.controller.module;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleMaintainOptionsEntity;
import com.sugon.entity.ModuleMaintainPlanEntity;
import com.sugon.modules.mold.service.IMoldMaintenanceManager;
import com.sugon.service.ModuleMaintainOptionsHisService;
import com.sugon.service.ModuleMaintainOptionsService;
import com.sugon.service.ModuleMaintainPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 模具维护规程选项表Controller
 *
 * @author sugon
 * @date 2019-08-09 08:58:39
 */
@Controller
@RequestMapping("modulemaintainoptions")
public class ModuleMaintainOptionsController {

    @Autowired
    private ModuleMaintainOptionsService moduleMaintainOptionsService;
    @Autowired
    private IMoldMaintenanceManager moduleMaintenanceManager;
    @Autowired
    private ModuleMaintainPlanService moduleMaintainPlanService;
    @Autowired
    private ModuleMaintainOptionsHisService moduleMaintainOptionsHisService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("modulemaintainoptions:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ModuleMaintainOptionsEntity> moduleMaintainOptionsList = moduleMaintainOptionsService.queryList2(query);
        int total = moduleMaintainOptionsService.queryTotal2(query);
        PageUtils pageUtil = new PageUtils(moduleMaintainOptionsList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("modulemaintainoptions:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleMaintainOptionsEntity moduleMaintainOptions = moduleMaintainOptionsService.queryObject(id);

        return R.ok().put("moduleMaintainOptions", moduleMaintainOptions);
    }

    /**
     * 保存
     */
    @AutoLog("模具维护规程选项表--保存")
    @RequestMapping("/save")
    @RequiresPermissions("modulemaintainoptions:save")
    @ResponseBody
    public R save(@RequestBody ModuleMaintainOptionsEntity moduleMaintainOptions) {
        boolean flag = moduleMaintainOptionsService.save(moduleMaintainOptions) > 0;
        if (flag) {
            // 记录设备变更履历
            moduleMaintainOptionsHisService.recordHistory("NEW", moduleMaintainOptions);
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("模具维护规程选项表--修改")
    @RequestMapping("/update")
    @RequiresPermissions("modulemaintainoptions:update")
    @ResponseBody
    public R update(@RequestBody ModuleMaintainOptionsEntity moduleMaintainOptions) {
        boolean flag = moduleMaintainOptionsService.update(moduleMaintainOptions) > 0;
        if (flag) {
            // 记录设备变更履历
            moduleMaintainOptionsHisService.recordHistory("UPDATE", moduleMaintainOptions);
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("模具维护规程选项表--无效时审核")
    @RequestMapping("/check")
    @RequiresPermissions("modulemaintainoptions:check")
    @ResponseBody
    public R check(@RequestBody Map<String, Object> map) {
        moduleMaintainOptionsService.check(map);
        return R.ok();
    }

    /**
     * 申请失效 哪些维护项
     */
    @AutoLog("模具维护规程选项表--无效选项库")
    @RequestMapping("/lose")
    @RequiresPermissions("modulemaintainoptions:lose")
    @ResponseBody
    public R lose(@RequestBody String[] ids) {
        for (String id : ids) {
            ModuleMaintainOptionsEntity mmo = moduleMaintainOptionsService.queryObject(id);
            mmo.setIsEffect(0);
            boolean flag = moduleMaintainOptionsService.update(mmo) > 0;
            if(flag){
                // 记录设备变更履历
                moduleMaintainOptionsHisService.recordHistory("APPLY_DISABLE", mmo);
            }
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具维护规程选项表--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("modulemaintainoptions:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return R.error("请选择规程项");
        }
        for (String id : ids) {
            ModuleMaintainOptionsEntity moduleMaintainOptions = moduleMaintainOptionsService.queryObject(id);
            if (moduleMaintainOptions != null) {
                // 记录设备变更履历
                moduleMaintainOptionsHisService.recordHistory("DELETE", moduleMaintainOptions);
                // 删除设备维护项
                moduleMaintainOptionsService.delete(id);
            }
        }
        return R.ok();
    }

    /**
     * 执行最近一次记录
     */
    @AutoLog("模具维护规程选项表--执行最近一次记录")
    @RequestMapping("/doOldChose/{planID}")
//    @RequiresPermissions("modulemaintainoptions:doOldChose")
    @ResponseBody
    public R doOldChose(@PathVariable("planID") String planID) {
        // 模具维护计划存在性校验
        ModuleMaintainPlanEntity moduleMaintainPlan = moduleMaintainPlanService.queryObject(planID);
        if (moduleMaintainPlan == null) {
            return R.error("模具维护计划不存在!");
        }
        String addResultMsg = moduleMaintenanceManager.addLastTimeModuleMaintenanceItem(moduleMaintainPlan);
        return StrUtil.isEmpty(addResultMsg) ? R.ok() : R.error(addResultMsg);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleMaintainOptionsEntity> list = moduleMaintainOptionsService.queryList2(params);

        return R.ok().put("list", list);
    }


    /**
     * 获取类型系统最大值
     *
     * @return
     */
    @RequestMapping("/loadMaxNum")
    @ResponseBody
    public R loadMaxNum(@RequestParam Map<String, Object> params) {
        int max = moduleMaintainOptionsService.loadMaxNum(params);
        return R.ok().put("max", max);
    }


    /**
     * excel导入
     */
    @RequestMapping("/import")
    @RequiresPermissions("modulemaintainoptions:import")
    @ResponseBody
    public R importE(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        int count = moduleMaintainOptionsService.importE(file);

        return R.ok("成功导入" + count + "条数据");
    }
}
